package com.sky.chapter1.threadCreate;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author : wushikai
 * <p>
 * date : 2021-03-20 晚
 * 使用callable 接口去 创建线程以获得 返回值;  注意: Callable 和相关类都是 属于 juc 包;
 *
 */
public class MyCall implements Callable<Integer> {


    /**
     * 可以获取返回值的
     * */
    @Override
    public Integer call() throws InterruptedException {

        System.out.println(  "线程 --> 启动");
        Thread.sleep(1000);
        return  1;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Callable<Integer> call = new  MyCall();
        FutureTask<Integer> task = new FutureTask<>(call);
        Thread thread = new Thread(task);
        thread.start();    // 必须让 callable 启动, 否则将获取不到返回值;

        //当线程执行完毕之后获得返回值
        Integer integer = task.get();  //如果线程没有启动, 则一直卡死在这里;
        System.out.println(integer);


    }

}
